

<template>
  <div>
    <van-nav-bar title="发布问题" left-arrow>
      <template #left>
        <van-icon name="wap-home-o" @click="back" style="font-size: 24px;margin-left: 20px" />
      </template>
    </van-nav-bar>
    <div>
      <van-field
          v-model="inputValue"
          label="问题搜索"
          placeholder="请输入内容"
          style="margin: 20px"
      ></van-field>

      <van-button type="primary" icon="search" style="margin-right: 10px;" @click="search">搜索</van-button>
      <van-button type="primary" icon="add" @click="addQuestion">新增问题</van-button>
    </div>

    <div>
      <van-cell-group inset>
        <van-cell v-for="q in questions" :key="q.id" style="background-color: ghostwhite; border-radius: 10px; margin-top: 20px" @click="goToAnswers(q.id)">
          <template #title>
            <div style="display: flex; justify-content: space-between; align-items: center;">
              <label style="font-weight: bold; font-size: 15px; margin-left: 10px;">{{ q.name }}</label>
              <span style="border-radius: 10px; padding: 5px;">{{ formatDate(q.ctime) }}</span>
            </div>
          </template>
          <template #label>
            <div style="display: flex; align-items: flex-start;">
              <label style="font-weight: bold; font-size: 20px; margin-left: 10px;">{{ q.details }}</label>
              <span style="border-radius: 10px; padding: 5px; margin-left: 150px; font-size: 15px;">数量：{{ q.count }}</span>
            </div>
          </template>
        </van-cell>
      </van-cell-group>
    </div>
  </div>
</template>
<!--<script setup>-->
<!--import { ref, onMounted } from "vue";-->
<!--import axios from "axios";-->
<!--import { useStore } from 'vuex'-->
<!--import { useRouter } from "vue-router";-->

<!--const store = useStore()-->
<!--const router = useRouter()-->
<!--const questions = ref([])-->
<!--const inputValue = ref('')-->

<!--onMounted(() => {-->
<!--  fetchData();-->
<!--})-->

<!--const fetchData = () => {-->
<!--  axios.get("question/list?uid=" + store.state.user.id).then(res => {-->
<!--    if (res.data.code == 0) {-->
<!--      questions.value = res.data.data;-->
<!--    }-->
<!--  });-->
<!--}-->

<!--const search = () => {-->
<!--  axios.get("question/querywhere?name=" + inputValue.value).then(res => {-->
<!--    if (res.data.code == 0) {-->
<!--      questions.value = res.data.data;-->
<!--    }-->
<!--  });-->
<!--}-->

<!--const addQuestion = () => {-->
<!--  router.push("/addquestion");-->
<!--}-->


<!--const back = () => {-->
<!--  history.back();-->
<!--}-->
<!--const formatDate = (dateTimeString) => {-->
<!--  const date = new Date(dateTimeString);-->
<!--  return date.toISOString().split("T")[0];-->
<!--}-->
<!--const goToAnswers = (questionId) => {-->
<!--  axios.get("question/queryById?id=" + questionId).then(res => {-->
<!--    if (res.data.code == 0) {-->
<!--      const question = res.data.data;-->
<!--      // 更新问题的count字段-->
<!--      question.count += 1;-->
<!--      axios.post("question/add", question).then(res => {-->
<!--        if (res.data.code === 0) {-->
<!--          // 跳转-->
<!--          router.push({ path: "/answers", query: { id: questionId } });-->
<!--        }-->
<!--      });-->
<!--    }-->
<!--  });-->
<!--}-->
<!--</script>-->
<script setup>
import { ref, onMounted } from "vue";
import axios from "axios";
import { useStore } from 'vuex'
import { useRouter } from "vue-router";

const store = useStore()
const router = useRouter()
const questions = ref([])
const inputValue = ref('')

onMounted(() => {
  fetchData();
})

const fetchData = () => {
  axios.get("question/list?uid=" + store.state.user.id).then(res => {
    if (res.data.code == 0) {
      questions.value = res.data.data;
    }
  });
}

const search = () => {
  axios.get("question/querywhere?name=" + inputValue.value).then(res => {
    if (res.data.code == 0) {
      questions.value = res.data.data;
    }
  });
}

const addQuestion = () => {
  router.push("/addquestion");
}


const back = () => {
  history.back();
}
const formatDate = (dateTimeString) => {
  const date = new Date(dateTimeString);
  return date.toISOString().split("T")[0];
}
const goToAnswers = (questionId) => {
  router.push({ path: "/answers", query: { id: questionId } });
}
</script>

<style scoped>

</style>

